package com.jia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.entity.SysPermission;
import com.jia.mapper.SysPermissionMapper;
import com.jia.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜佳佳
 * @since 2021-05-23
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Override
    public List<SysPermission> getListByPermissions(List<String> collect) {

        if (collect.size() <= 0){
            return null;
        }
        List<SysPermission> list = new ArrayList<>();
        collect.forEach(item->{
            QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
            wrapper.eq("permission",item);
            SysPermission sysPermission = baseMapper.selectOne(wrapper);
            list.add(sysPermission);
        });
        return list;
    }

    @Override
    public List<SysPermission> getByParentId(String parentId) {

        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId).orderByAsc("orders");
        List<SysPermission> sysPermissions = baseMapper.selectList(wrapper);
        sysPermissions.forEach(item -> {
            List<SysPermission> childList = getByParentId(item.getId());
            if (childList.size() > 0){
                item.setHasChildren(true);
            }else {
                item.setHasChildren(false);
            }
        });

        return sysPermissions;
    }
}
